
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author BENSON
 */
public class ControllerAddOrder {
    Connection conn=null;
    String data="";
    String querry;
    Statement st=null;
   public EnterForm EnterOrder;
    
    private ActionListener actionListener;
    
    public ControllerAddOrder( ){
        EnterOrder = new EnterForm();
        EnterOrder.setVisible(true);
        showFlights();
                          
    }
    
    public String getClickedFlight(){
    return data;
    }
    
     public void showFlights(){
         
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Flights ;" ;
        st=conn.prepareStatement(querry);
        st.execute("USE jetwingdb;");

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
   EnterOrder.getFlightNo().addItem(rs.getString("fName"));
  
   }
   st.close();
   conn.close();
   rs.close();
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    }
   
     
    public void addOrders(){
    try{
       Class.forName("com.mysql.jdbc.Driver");
       conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
       
        querry=("INSERT INTO orders SET Name=?,passPortNo=? ,email =?,phoneNo =?,flightNo =?,seatNo=? ,fFrom =?,destination =?,depatureTime =?, arrivalTime=?;");
        
    PreparedStatement p =conn.prepareStatement(querry);
    p.setString(1, EnterOrder.getFName().getText());
    p.setString(2, EnterOrder.pasportNo().getText());
    p.setString(3, EnterOrder.getEmail().getText());
    p.setString(4, EnterOrder.getPhoneNo().getText());
    p.setString(5, (EnterOrder.getFlightNo().getSelectedItem()).toString());
    p.setString(6, EnterOrder.getSeatNo().getText());
    p.setString(7, EnterOrder.getFrom().getText());
    p.setString(8, EnterOrder.getDestination().getText());
    p.setString(9, EnterOrder.getDptTime().getText());
    p.setString(10, EnterOrder.getArrivalTime().getText());
    p.execute();
               
    p.close();
   conn.close();
  JOptionPane.showMessageDialog(null, "Order has been succesfully added");
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    
    }
    public void contolCombo(){        
        actionListener = new ActionListener() {
            
              public void actionPerformed(ActionEvent actionEvent) {  
                  if (EnterOrder.getFlightNo().getSelectedIndex() != -1) {                     
               data = (EnterOrder.getFlightNo().getItemAt(EnterOrder.getFlightNo().getSelectedIndex())).toString();
               setDetails(data);
            }
              }
              
        };                
        EnterOrder.getFlightNo().addActionListener(actionListener);
    }
     public void setDetails(String d){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT fFrom,destination,depatureTime,arrivalTime FROM Flights WHERE fName LIKE '%"+d+"%';" ; 
        st=conn.prepareStatement(querry);
        st.execute("USE jetwingdb;");

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
    EnterOrder.getFrom().setText(rs.getString("fFrom"));
    EnterOrder.getDestination().setText(rs.getString("destination"));
    EnterOrder.getDptTime().setText(rs.getString("depatureTime"));
    EnterOrder.getArrivalTime().setText(rs.getString("arrivalTime"));
     
   }
   st.close();
   conn.close();
  
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    }
    
     public void contol(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==EnterOrder.getAddButton()){
                        addOrders();
                  }
                  if(actionEvent.getSource()==EnterOrder.getCloseButton()){
                  EnterOrder.setVisible(false);
                  
                  }
                  if(actionEvent.getSource()==EnterOrder.getFlightNo()){
                  showFlights();
                  
                  }
              }
              
        };                
        EnterOrder.getAddButton().addActionListener(actionListener);
        EnterOrder.getCloseButton().addActionListener(actionListener);
        
    }
}
